<?php

/**
 * LoginForm class.
 * LoginForm is the data structure for keeping
 * user login form data. It is used by the 'login' action of 'SiteController'.
 */
class AccountForm extends CFormModel
{
	public $name;
	public $dob_year;
	public $dob_month;
	public $dob_day;
	public $birthday;
	public $phone;
	public $gender;
	public $email;
	public $privacy;
	public $seo_name;
	
	public $usermeta;

	/**
	 * Declares the validation rules.
	 * The rules state that username and password are required,
	 * and password needs to be authenticated.
	 */
	public function rules()
	{
		return array(
			array('name', 'required'),
			array('name', 'length', 'allowEmpty' => true, 'max' => 25, 'tooLong' => 'Chỉ cho phép tối đa 25 ký tự'),
			array('name, phone, usermeta, privacy', 'safe'),
			array('dob_year', 'numerical', 'min' => 1920, 'max' => date('Y')),
			array('dob_month', 'numerical', 'min' => 1, 'max' => 12),
			array('dob_day', 'numerical', 'min' => 1, 'max' => 31),
			array('gender', 'in', 'range' => array('M', 'F', 'O'), 'allowEmpty' => false),
			array('seo_name', 'length', 'allowEmpty' => true, 'max' => 20, 'tooLong' => 'Chỉ cho phép tối đa 20 ký tự'),
			array('seo_name', 'match', 'allowEmpty' => true, 'pattern' => '/^[\.a-z0-9_-]+$/u'),
//			array('seo_name', 'unique', 'allowEmpty' => true, 'caseSensitive' => false, 'className' => 'User', 'attributeName' => 'seo_name'),
			array('seo_name', 'custom_unique', 'allowEmpty' => true, 'caseSensitive' => false, 'attributeName' => 'seo_name'),
		);
	}
	
	public function custom_unique($attribute, $params) {

		$value = $this->$attribute;
		if( @$params['allowEmpty'] && $this->isEmpty($value))
			return;

		$criteria = new CDbCriteria(array(
			'condition' => @$params['caseSensitive'] ? "$attribute=:value" : "LOWER($attribute)=LOWER(:value)",
			'params' => array(
				':value'=>$value,
				':user_id' => user()->id,
			),
		));
		
		$criteria->addCondition('user_id!=:user_id');
		
		$finder = User::model();
		$exists = $finder->exists($criteria);

		if ( $exists ) {
			$message = @$params['message'] !== null ? $params['message'] : Yii::t('yii','{attribute} "{value}" has already been taken.');
			$this->addError($attribute, strtr($message, array('{value}' => CHtml::encode($value), '{attribute}' => $this->getAttributeLabel($attribute) )));
		}
	}
	
	protected function isEmpty($value,$trim=false)
	{
		return $value===null || $value===array() || $value==='' || $trim && is_scalar($value) && trim($value)==='';
	}

	/**
	 * Declares attribute labels.
	 */
	public function attributeLabels()
	{
		return array(
			'seo_name'	=> t('Tên người dùng'),
			'name'		=> t('Name'),
			'phone'		=> t('Phone'),
			'dob'		=> t('DOB'),
			'birthday'	=> t('Birthday'),
			'gender'	=> t('Gender'),
			'email'		=> t('Email'),
			'privacy'	=> t('Ai có thể xem trang cá nhân của bạn'),
			'love_category' => t('Thể loại yêu thích'),
			'love_author'	=> t('Tác giả yêu thích'),
		);
	}
	
	public function loadFromUser($user) {
		
		$this->attributes = $user->attributes;
		
		$this->email = $user->email;
		if ( $user->birthday ) {
			$dob = $user->birthday;
		} else {
			$dob = strtotime('01-01-1980');
		}
		
		$this->dob_year = date('Y', $dob);
		$this->dob_month = date('n', $dob);
		$this->dob_day = date('j', $dob);
		
		$this->usermeta = $user->getUserMeta();
	}
}
